﻿using log4net;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Configuration;
using System.Security.Policy;
using System.Windows;
using System.Windows.Media;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static ILog log = LogManager.GetLogger(typeof(MainWindow));
        private String loginUrl = ConfigurationManager.AppSettings["loginUrl"];


        public MainWindow()
        {
            log.Debug("MainWindow Init.");
            InitializeComponent();

            #region 启动时串口最大化显示
            Rect rc = SystemParameters.WorkArea; //获取工作区大小
            this.Left = 0; //设置位置
            this.Top = 0;
            this.Width = rc.Width;
            this.Height = rc.Height;
            #endregion

            log.Debug("Height:" + rc.Height);
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            log.Debug("Window_Loaded start.");
            webView0.NavigationCompleted += WebView_login;
            webView0.Source = new Uri(loginUrl);
            log.Debug("Window_Loaded end.");

        }

        private void WebView_login(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            log.Debug("WebView_login start.");
            webView0.NavigationCompleted -= WebView_login;
            webView0.NavigationCompleted += WebView_NavigationCompleted;


            webView0.CoreWebView2.ExecuteScriptAsync("$(\"[type='Password']\").val('1')");
            webView0.CoreWebView2.ExecuteScriptAsync("$(\"[name='AccountNumber']\").val('998')");
            webView0.CoreWebView2.ExecuteScriptAsync("$(\"#submitBtn\").click()");

            log.Debug("WebView_login start.");
        }

        private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            log.Debug("WebView_NavigationCompleted start.");
            webView0.NavigationCompleted -= WebView_NavigationCompleted;
            webView0.Visibility = Visibility.Hidden;

            try
            {
                int childrenCount = VisualTreeHelper.GetChildrenCount(this.grid);
                // 遍历所有浏览器
                for (int i = 1; i < childrenCount; i++)
                {
                    var childBorder = VisualTreeHelper.GetChild(this.grid, i);
                    if(i > 9) { continue; }

                    var child = VisualTreeHelper.GetChild(childBorder, 0);
                    if (child.GetType().Name == "WebView2")
                    {
                        var webView = child as Microsoft.Web.WebView2.Wpf.WebView2;
                        // await webView.EnsureCoreWebView2Async();
                        webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;

                         String url = ConfigurationManager.AppSettings[webView.Name];
                        webView.CoreWebView2.Navigate(url);
                        log.Debug("webView:" + webView.Name + " =" + url);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("WebView_NavigationCompleted Exception.", ex);
            }


            log.Debug("WebView_NavigationCompleted end.");
        }
    }
}
